﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Security.Cryptography;

namespace PRE.Salary
{
    public partial class FrmCapnhatmucluong : DevExpress.XtraEditors.XtraForm
    {
        #region Variable
        DAL.EntitiesDAL.MucluongDAL _dal = new DAL.EntitiesDAL.MucluongDAL();
        BLL.EntitiesBLL.MucluongBLL _bll = new BLL.EntitiesBLL.MucluongBLL();

        DataTable _dtb = new DataTable();
        #endregion

        #region Inits
        public FrmCapnhatmucluong()
        {
            InitializeComponent();
        }
        #endregion

        #region ReadOnlyControl
        protected void ReadOnlyControl(bool isReadOnly = true)
        {
            txt_Mucluong.Properties.ReadOnly = isReadOnly;           
        }
        #endregion

        #region ClearDataBindings
        protected void ClearDataBindings()
        {
            txt_Mucluong.DataBindings.Clear();           
        }
        #endregion

        #region DataBindingControl
        protected void DataBindingControl()
        {
            txt_Mucluong.DataBindings.Add("Text", _dtb, ".MUCLUONG");           
        }
        #endregion

        #region PerformRefresh
        protected  void PerformRefresh()
        {
            ReadOnlyControl();
            txt_Mucluong.ResetText();
            bto_Them.Visible = true;
            bto_Xoa.Visible = true;
            LoadData();           

            if (_dtb != null)
            {
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            
        }
        #endregion

        #region LoadData
        protected void LoadData()
        {
            bto_Luu.Visible = false;
            bto_Huy.Visible = false;
            try
            {
                _dtb = _dal.Select();

                if (_dtb != null)
                {                    
                    grcMucluong.DataSource = _dtb;

                    AppGridView.SummaryField(grvMucluong, "MUCLUONG", "Tổng = {0:#,0} Mức lương cơ bản", DevExpress.Data.SummaryItemType.Count);

                    grvMucluong.OptionsView.ColumnAutoWidth = true;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Lỗi : " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Form Load
        private void grcMucluong_Load_1(object sender, EventArgs e)
        {
            PerformRefresh();
        }
        #endregion

        #region Thoát
        private void bto_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Thêm
        private void bto_Them_Click(object sender, EventArgs e)
        {
            txt_Mucluong.Properties.ReadOnly = false;
            txt_Mucluong.ResetText();
            txt_Mucluong.Focus();
            bto_Them.Visible = false;
            bto_Xoa.Visible = false;
            bto_Luu.Visible = true;
            bto_Huy.Visible = true;
        }
        #endregion

        #region Luu
        private void bto_Luu_Click(object sender, EventArgs e)
        {
            var o = new DAL.Entities.Mucluong()
            {
                MUCLUONG =Convert.ToString(txt_Mucluong.Text)
            };

            var oki = _dal.Insert(o);
            if (oki)
            {
                XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                PerformRefresh();             
               
            }
            else
            {
                XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
        #endregion

        #region Hủy
        private void bto_Huy_Click(object sender, EventArgs e)
        {
            PerformRefresh();
            bto_Huy.Visible = false;
            bto_Luu.Visible = false;           
            txt_Mucluong.Properties.ReadOnly = true;
        }
        #endregion

        #region Xóa
        private void bto_Xoa_Click(object sender, EventArgs e)
        {
            var id1 = grvMucluong.GetFocusedRowCellValue("MUCLUONG") + "";

            DialogResult result;
            var ok = _dal.Delete(id1);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
    }
}
